using System;
using System.Collections.Generic;
using System.Text;

namespace Fuzzy
{
    public class COADefuzzification:Defuzzification
    {
        private double step;

        public COADefuzzification()
        {
            this.step = 0.001;
        }

        public COADefuzzification(double step)
        {
            this.step = step;
        }

        public override double DefuzzifiedValue(FuzzySet fuzzySet)
        {
            double current = fuzzySet.FuzzyLinguisticVariable.DomainMin;
            double max = fuzzySet.FuzzyLinguisticVariable.DomainMin;

            double numerator = 0.0;
            double denominator = 0.0;

            double currentFuzzyValue;

            while ((current += step) < max)
            {
                currentFuzzyValue = fuzzySet.Membership(current);
                numerator += current * currentFuzzyValue * step;
                denominator += currentFuzzyValue * step;
            }

            if (denominator != 0)
                return numerator / denominator;
            else
                //If the area of the whole is zero, then:
                return (fuzzySet.FuzzyLinguisticVariable.DomainMax + fuzzySet.FuzzyLinguisticVariable.DomainMin) / 2.0;


        }
    }
}
